package com.atguigu.interactive;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

////V1  synchronized +wait+notifyAll传统老版本
//class AirCondition{
//    private int num=0;//有一个初始值为零的变量
//    public synchronized void increment()throws InterruptedException{
//    //判断
//        while(num!=0){
//            this.wait();
//        }
//        //干活
//        System.out.println(Thread.currentThread().getName()+(++num));
//        this.notifyAll();
//    }
//    public synchronized void decrement()throws InterruptedException{
//        //判断
//        while(num==0){
//            this.wait();
//        }
//        //干活
//        System.out.println(Thread.currentThread().getName()+(--num));
//        //通知
//        this.notifyAll();
//    }
//}

        //V2JUC新版本,lock+condition.await()+condition.signalAll
class AirCondition{
    private int num=0;  //有一个初始值为零的变量
            final Lock lock = new ReentrantLock();
            final Condition condition = lock.newCondition();
            public void increment() throws InterruptedException{
                lock.lock();
                try {
                    while (num != 0) {
                        condition.await();
                    }

                    System.out.println(Thread.currentThread().getName() + (++num));
                    condition.signalAll();
                }finally {
                    lock.unlock();
                }
            }

            public void decrement() throws InterruptedException{
                lock.lock();
                try {
                    while (num == 0) {
                        condition.await();
                    }

                    System.out.println(Thread.currentThread().getName() +( --num));
                    condition.signalAll();
                }finally {
                    lock.unlock();
                }
            }
        }

public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition ac = new AirCondition();

        new Thread(()->{
            try{
                for(int i=0;i<10;i++){
                    try{TimeUnit.MILLISECONDS.sleep(100);}catch(InterruptedException e){e.printStackTrace();}
                    ac.increment();
                }

            }catch(InterruptedException e){throw new RuntimeException(e);}
        },"A").start();

        new Thread(()->{
            try{
                for(int i=0;i<10;i++){
                    try{TimeUnit.MILLISECONDS.sleep(200);}catch(InterruptedException e){e.printStackTrace();}
                    ac.decrement();
                }

            }catch(InterruptedException e){throw new RuntimeException(e);}
        },"B").start();

        new Thread(()->{
            try{
                for(int i=0;i<10;i++){
                    try{TimeUnit.MILLISECONDS.sleep(300);}catch(InterruptedException e){e.printStackTrace();}
                    ac.increment();
                }

            }catch(InterruptedException e){throw new RuntimeException(e);}
        },"C").start();
        new Thread(()->{
            try{
                for(int i=0;i<10;i++){
                    try{TimeUnit.MILLISECONDS.sleep(400);}catch(InterruptedException e){e.printStackTrace();}
                    ac.decrement();
                }

            }catch(InterruptedException e){throw new RuntimeException(e);}
        },"D").start();

}
}
